package org.bear.array;

import java.util.HashSet;
import java.util.Set;

//3.无重复字符的最长子串
/*
s = "abcabcbb", 3
tmmzuxt  5
 */
public class LengthOfLongestSubstring {

    public int lengthOfLongestSubstring(String s) {
        int left = 0;
        int right = 0;
        int maxLen = 0;
        Set<Character> set = new HashSet<>();
        while (right < s.length()) {
            char c = s.charAt(right);
            if (set.contains(c)) {
                while (left<right && s.charAt(left) != c) {
                    set.remove(s.charAt(left++));
                }
                left++;
            }else {
                set.add(c);
                maxLen = Math.max(maxLen, right - left + 1);
            }
            right++;
        }
        return maxLen;
    }

    public static void main(String[] args) {
        LengthOfLongestSubstring s = new LengthOfLongestSubstring();
        System.out.println(s.lengthOfLongestSubstring("tmmzuxt"));
    }
}
